﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace GPMS.Web.Controllers
{
    using GPMS.Model;
    using GPMS.BLL;
    using Webdiyer.WebControls.Mvc;
    /// <summary>
    /// 站内信息控制器
    /// </summary>
    public class MessageController : BaseController
    {
        //
        // GET: /Message/

        public ActionResult Index()
        {
            ViewData["NotRead"] = MessageHelper.GetNotReadCount(AccountHelper.GetLoginUserID(Request), gpmsDb);
            return View();
        }

        /// <summary>
        /// 发送信息
        /// </summary>
        /// <returns></returns>
        public ActionResult Send(Guid? id)
        {
            Guid userid = GPMS.BLL.AccountHelper.GetLoginUserID(Request);
            var files = GPMS.BLL.FileHelper.GetUserFileList(userid, gpmsDb);
            files.Insert(0, new File() { FileID = Guid.Empty, Name = "---无---" });
            ViewData["files"] = files;
            MessageVM model = new MessageVM();
            model.Message = new Message();
            if (id!=null)
            {
                model.ReceiverName = AccountHelper.Get(id.Value,gpmsDb).UserName;
            }
            return View(model);
        }

        /// <summary>
        /// 发送信息处理
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public ActionResult Send(FormCollection collection)
        {
            try
            {
                MessageVM model = new MessageVM();
                model.SenderName = "信息已发出";
                UpdateModel(model, collection);
                MessageHelper.Add(model, gpmsDb,Request);
                return Success(null);
            }
            catch (Exception ex)
            {
                return JsonError("发送失败:" + ex.Message);
            }
            
        }

        /// <summary>
        /// 查看接收信息
        /// </summary>
        /// <returns></returns>
        public ActionResult ReceiveMessages(int? id)
        {
            //禁止页面被缓存
            Response.Cache.SetCacheability(HttpCacheability.NoCache);
            Response.Cache.SetExpires(DateTime.Today.AddYears(-2));
            PagedList<MessageVM> list = MessageHelper.GetListReceive(AccountHelper.GetLoginUserID(Request), id ?? 1, gpmsDb);
            if (Request.IsAjaxRequest())
            {
                return PartialView("ReceiveMessageList", list);
            }
           return View(list);
        }
        /// <summary>
        /// 查看已发送信息
        /// </summary>
        /// <returns></returns>
        public ActionResult SendedMessages(int? id)
        {
            //禁止页面被缓存
            Response.Cache.SetCacheability(HttpCacheability.NoCache);
            Response.Cache.SetExpires(DateTime.Today.AddYears(-2));
            PagedList<MessageVM> list = MessageHelper.GetListSend(AccountHelper.GetLoginUserID(Request), id ?? 1, gpmsDb);
            if (Request.IsAjaxRequest())
            {
                return PartialView("SendMessageList", list);
            }
            return View(list);
        }

        public ActionResult NotReadMessages(int? id)
        {
            //禁止页面被缓存
            Response.Cache.SetCacheability(HttpCacheability.NoCache);
            Response.Cache.SetExpires(DateTime.Today.AddYears(-2));
            PagedList<MessageVM> list = MessageHelper.GetListNotRead(AccountHelper.GetLoginUserID(Request), id ?? 1, gpmsDb);
            if (Request.IsAjaxRequest())
            {
                return PartialView("ReceiveMessageList", list);
            }
            return View(list);
        }

        /// <summary>
        /// 查看具体信息
        /// </summary>
        /// <returns></returns>
        public ActionResult MessageDetail(Guid id)
        {
            return View(MessageHelper.Get(AccountHelper.GetLoginUserID(Request),id, gpmsDb));
        }

        


        public ActionResult DeleteReceive(Guid id)
        {
            return Confirm("您确定删除此条信息吗？");
        }

        [HttpPost]
        public ActionResult DeleteReceive(Guid id,FormCollection collection)
        {
            try
            {
                MessageHelper.DeletedReceiver(id, gpmsDb);
                return Success(null);
            }
            catch (Exception ex)
            {
                return JsonError("更新失败:" + ex.Message);
            }
        }

        public ActionResult DeleteSend(Guid id)
        {
            return Confirm("您确定删除此条信息吗？");
        }

        [HttpPost]
        public ActionResult DeleteSend(Guid id, FormCollection collection)
        {
            try
            {
                MessageHelper.DeletedSender(id, gpmsDb);
                return Success(null);
            }
            catch (Exception ex)
            {
                return JsonError("更新失败:" + ex.Message);
            }
        }
    }
}
